package com.ywdnf.gomoku.util;

import java.util.UUID;
import org.springframework.util.StringUtils;

/**
 * TODO
 *
 * @author lss
 * @date 2019-02-26, Tue
 */
public class IdUtils {

    private static final String DEFAULT_SEPARATOR = "-";

    /**
     * 获取32位id
     */
    public static String get32Id() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getLenId(int len) {
        return len >= 32 ? get32Id() : get32Id().substring(0, (len <= 0 ? 8 : len));
    }

    public static String get8Id() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String specifyPrefixAndLengthId(String prefix, String separator, int len) {
        if (StringUtils.isEmpty(separator)) {
            separator = DEFAULT_SEPARATOR;
        }
        return (prefix + separator + get32Id()).substring(0, len + separator.length() + prefix.length());
    }

    public static String specifyPrefixAndLengthId(String prefix, int len) {
        return (prefix + get32Id()).substring(0, len + prefix.length());
    }

}
